const { ArticleModel, CollectionModel, ZanModel, UserModel, LabelModel } = require('../model/DataModel')

class ArticleService {
  async save(data) {
    const { labelId } = data
    const article = await ArticleModel.create(data)
    if (article == null) return article
    const label = await LabelModel.findOne({ _id: labelId })
    label.article_count++
    label.save()
    return article
  }

  async findArticleDetail(id) {
    const article = await ArticleModel.findOne({ _id: id })
      .populate('userId', 'name')
      .populate('categoryId', 'categoryName')
      .populate('labelId', 'label')
      .exec()
    article.read++
    article.hot++
    article.save()
    return article

    // ArticleModel.updateOne({ _id: id }, { read: article.read++, hot: article.hot++ }).exec((err, res) => {
    //   console.log(err, res)
    // })
    // const article2 = await ArticleModel.findOne({ _id: id })
    // console.log(article2)
    // return article
  }


  async getArticle(page = 1, size = 10) {
    return await ArticleModel.find({}, { content: 0 }).skip((page - 1) * size)
      .limit(size)
      .populate('userId', 'name')
      .populate('categoryId', 'categoryName')
      .populate('labelId', 'label')
      .sort('-hot')
      .sort('updatedAt')
      .exec()

  }

  async findByName(name, page = 1, size = 10) {
    const reg = new RegExp(name, 'i')

    const article = await ArticleModel.find({ $or: [{ title: { $regex: reg } }] }, { content: 0 })
      .skip((page - 1) * size)
      .limit(size)
      .populate('userId', 'name')
      .populate('categoryId', 'categoryName')
      .populate('labelId', 'label')
      .exec()

    const user = await UserModel.find({ $or: [{ name: { $regex: reg } }] }, { password: 0 })
      .skip((page - 1) * size)
      .limit(size)
      .exec()
    return { article, user }
  }

  async findByCategory(categoryId, page = 1, size = 10) {

    return await ArticleModel.find({ categoryId }, { content: 0 })
      .skip((page - 1) * size)
      .limit(size)
      .populate('userId', 'name')
      .populate('categoryId', 'categoryName')
      .populate('labelId', 'label')
      .exec()
  }

  async findByLabel(labelId, page = 1, size = 10) {

    return await ArticleModel.find({ labelId }, { content: 0 })
      .skip((page - 1) * size)
      .limit(size)
      .populate('userId', 'name')
      .populate('categoryId', 'categoryName')
      .populate('labelId', 'label')
      .exec()
  }

  async findByUser(userId, page = 1, size = 10) {

    return await ArticleModel.find({ userId }, { content: 0 })
      .skip((page - 1) * size)
      .limit(size)
      .populate('userId', 'name')
      .populate('categoryId', 'categoryName')
      .populate('labelId', 'label')
      .exec()
  }

  async removeArticle(articleId) {
    return await ArticleModel.deleteOne({ _id: articleId })
  }

  async addZan(userId, articleId) {
    // const zan = await ZanModel.findOne({ _id: zanId })
    // const article = await ArticleModel.findOne({ _id: articleId })
    // if (Object.keys(zan).length === 0) {
    //   const res = await ZanModel.create({ userId, articleId })
    //   ArticleModel.update({ _id: articleId }, { zan: article.zan++, hot: article.hot++ })
    //   return res
    // }
    // const res = await ZanModel.update({ _id: zanId }, { zanStatus: 1 })
    // ArticleModel.update({ _id: articleId }, { zan: article.zan++, hot: article.hot++ })
    // return res
    const article = await ArticleModel.findOne({ _id: articleId })
    const res = await ZanModel.find({ userId, articleId })
    if (res.length !== 0) return
    const zan = await ZanModel.create({ userId, articleId })
    article.hot++
    article.zan++
    article.save()
    return zan
  }

  async cancelZan(userId, articleId) {
    const article = await ArticleModel.findOne({ _id: articleId })
    const res = await ZanModel.deleteOne({ userId, articleId })
    if (res.n == 0) return
    article.hot--;
    article.zan--;
    article.save()
    return res
  }

  async addCollection(userId, articleId, collectionName) {
    return await CollectionModel.create({ userId, articleId, collectionName })
  }

  async removeCollection(id) {
    return await CollectionModel.remove({ _id: id })
  }

  async renameCollection(id, collectionName) {
    return await CollectionModel.update({ _id: id }, { collectionName })
  }
}

module.exports = new ArticleService()